package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;
    //查询所有
    @Override
    public List<Address> findList() {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId, UserHolder.get().getId());
        List<Address> addressList = addressMapper.selectList(wrapper);
        return addressList;
    }
    //添加
    @Override
    public void save(Address address) {
        address.setUserId(UserHolder.get().getId());
        addressMapper.insert(address);
    }
    //修改
    @Override
    public Address setDefault(Long id) {
        //修改当前所有地址的is_default=0
        //update address set is_default=0 where user_id=登录用户id
        //封装更新字段
        Address address1 = new Address();
        address1.setIsDefault(0);
        //设置更新条件
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId,UserHolder.get().getId());
        addressMapper.update(address1,wrapper);
        //指定id的is_default=1
        //update address set is_default=1 where id = ?
        Address address2 = new Address();
        address2.setUserId(UserHolder.get().getId());
        address2.setIsDefault(1);
        address2.setId(id);
        addressMapper.updateById(address2);
        return address2;
    }
    //查询默认地址
    @Override
    public Address findDefault() {
        //select * from address where is_default=1 and user_id=?
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getUserId,UserHolder.get().getId());
        wrapper.eq(Address::getIsDefault,1);
        return addressMapper.selectOne(wrapper);
    }
    //回显
    @Override
    public Address findById(Long id) {
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getId,id);
        Address address = addressMapper.selectOne(wrapper);
        return address;
    }
    //更新
    @Override
    public void update(Address address) {
        //构建条件
        LambdaQueryWrapper<Address> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Address::getConsignee,address.getConsignee());
        wrapper.eq(Address::getDetail,address.getDetail());
        wrapper.eq(Address::getLabel,address.getLabel());
        wrapper.eq(Address::getSex,address.getSex());
        addressMapper.updateById(address);
    }
    //删除
    @Override
    public void deleteById(Long ids) {
        addressMapper.deleteById(ids);
    }
}
